// import router from './router'
// import store from './store'
// import { Message } from 'element-ui'
// import NProgress from 'nprogress' // progress bar
// import 'nprogress/nprogress.css' // progress bar style
// import { getToken } from '@/utils/auth' // get token from cookie
// import getPageTitle from '@/utils/get-page-title'

// NProgress.configure({ showSpinner: false }) // NProgress Configuration

// const whiteList = ['/login'] // no redirect whitelist

// router.beforeEach(async(to, from, next) => {
//   // start progress bar
//   NProgress.start()

//   // set page title
//   document.title = getPageTitle(to.meta.title)

//   // determine whether the user has logged in
//   const hasToken = getToken()

//   if (hasToken) {
//     if (to.path === '/login') {
//       // if is logged in, redirect to the home page
//       next({ path: '/' })
//       NProgress.done()
//     } else {
//       const hasGetUserInfo = store.getters.name
//       if (hasGetUserInfo) {
//         next()
//       } else {
//         try {
//           // get user info
//           await store.dispatch('user/getInfo')

//           next()
//         } catch (error) {
//           // remove token and go to login page to re-login
//           await store.dispatch('user/resetToken')
//           Message.error(error || 'Has Error')
//           next(`/login?redirect=${to.path}`)
//           NProgress.done()
//         }
//       }
//     }
//   } else {
//     /* has no token*/

//     if (whiteList.indexOf(to.path) !== -1) {
//       // in the free login whitelist, go directly
//       next()
//     } else {
//       // other pages that do not have permission to access are redirected to the login page.
//       next(`/login?redirect=${to.path}`)
//       NProgress.done()
//     }
//   }
// })

// router.afterEach(() => {
//   // finish progress bar
//   NProgress.done()
// })
import router, { asyncRoutes } from './router'
import store from '@/store'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import getPageTitle from '@/utils/get-page-title'
// 白名单
const whiteList = ['/login', '/404']
// 前置守卫
router.beforeEach(async(to, from, next) => {
  document.title = getPageTitle(to.meta.title)
  // 显示进度条
  NProgress.start()
  const token = store.state.user.token
  if (token) {
    if (to.path === '/login') {
      next('/')
      NProgress.done()
    } else {
      // 获取用户信息
      // 需要有token跳转之前
      // 1. 先判断有没有yoghurt信息(在第一次访问时,用户信息已经存放到Vuex中,可以用Vuex中取到用户信息进行判断)
      // 2.没有用户信息,需要获取用户信息(权限信息),获取到以后存到Vuex中
      if (!store.getters.userId) {
        const menus = await store.dispatch('user/getUserInfo')
        // 3.使用[后端返回的权限信息] 和本地提前预置的动态路由表进行对比筛选,筛选出当前用户邮访问的菜单
        const filterRoutes = asyncRoutes.filter(item => {
          const routeName = item.children[0].name
          return menus.includes(routeName)
        })
        filterRoutes.push({ path: '*', redirect: '/404', hidden: true })
        // 4.将筛选好的用户有权访问路由动态添加到路由对象中
        // 通过addRoutes添加的路由是存放到内存中的不会对代码造成影响
        router.addRoutes(filterRoutes)
        // 将动态路由添加后,将动态路由存放到Vuex 中
        store.commit('permission/setRoutes', filterRoutes)

        next({
          path: to.path
        })
      }
      // 5.直接放行 next()
      next()
      NProgress.done()
    }
  } else {
    if (whiteList.includes(to.path)) {
      next()
    } else {
      next('/login')
      NProgress.done()
    }
  }
})
// 后置守卫
router.afterEach((to, from) => {
  NProgress.done()
})
